home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 028a / unzup41e.zip / MATCH.C < prev    next >
C/C++ Source or Header  |  1990-12-04  |  5KB  |  222 lines

  1. /*--------------------------------------------------------------------------
  2.  
  3.   match.c
  4.  
  5.   The match() routine recursively compares a string to a "pattern" (regular
  6.   expression), returning TRUE if a match is found or FALSE if not.  This
  7.   version is specifically for use with unzip.c:  it leaves the case (upper,
  8.   lower, or mixed) of the string alone, but converts any uppercase characters
  9.   in the pattern to lowercase if indicated by the global variable lcflag
  10.   (which is to say, string is assumed to have been converted to lowercase
  11.   already, if such was necessary).
  12.  
  13.   --------------------------------------------------------------------------
  14.  
  15.   Revision history:
  16.  
  17.      Original Author:  Thom Henderson
  18.      Original System V port:  Mike Stump
  19.  
  20.      03/22/87  C. Seaman      enhancements, bug fixes, cleanup
  21.      11/13/89  C. Mascott     adapted for use with unzip
  22.      01/25/90  J. Cowan       made case-insensitive (only for smart toupper())
  23.      03/17/90  D. Kirschbaum  prototypes, other tweaks for Turbo C
  24.      05/18/90  M. O'Carroll   DOS and OS/2 family version
  25.      09/20/90  G. Roelofs     modified for lcflag, moved stuff to header file
  26.  
  27.   --------------------------------------------------------------------------
  28.  
  29.   Copyright, originally from arcmatch.c, version 1.1 (?):
  30.  
  31.      * ARC - Archive utility - ARCMATCH
  32.      *
  33.      * Version 2.17, created on 12/17/85) at 20:32:18
  34.      *
  35.      * (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  36.  
  37.   --------------------------------------------------------------------------*/
  38.  
  39.  
  40. #include "unzip.h"
  41.  
  42.  
  43. /*******************/
  44. /*  Match Defines  */
  45. /*******************/
  46.  
  47. #define ASTERISK        '*'     /* The '*' metacharacter */
  48. #define QUESTION        '?'     /* The '?' metacharacter */
  49. #define BACK_SLASH      '\\'    /* The '\' metacharacter */
  50. #define LEFT_BRACKET    '['     /* The '[' metacharacter */
  51. #define RIGHT_BRACKET   ']'     /* The ']' metacharacter */
  52. #define EOS             '\000'  /* end-of-string */
  53.  
  54. #define IS_OCTAL(ch)    (ch >= '0' && ch <= '7')
  55.  
  56.  
  57.  
  58. /********************/
  59. /*  Match Typedefs  */
  60. /********************/
  61.  
  62. typedef short int INT;
  63. typedef short int BOOLEAN;
  64.  
  65.  
  66.  
  67. /*************************************/
  68. /*  Match Local Function Prototypes  */
  69. /*************************************/
  70.  
  71. static BOOLEAN do_list __((register char *string, char *pattern));
  72. static void list_parse __((char **patp, char *lowp, char *highp));
  73. static char nextch __((char **patp));
  74.  
  75.  
  76.  
  77.  
  78.  
  79. /**********************/
  80. /*  Function match()  */
  81. /**********************/
  82.  
  83. int match(string, pattern)
  84. char *string;
  85. char *pattern;
  86. {
  87.     register int ismatch;
  88.  
  89.     ismatch = FALSE;
  90.     switch (*pattern) {
  91.     case ASTERISK:
  92.         pattern++;
  93.         do {
  94.             ismatch = match(string, pattern);
  95.         }
  96.         while (!ismatch && *string++ != EOS);
  97.         break;
  98.     case QUESTION:
  99.         if (*string != EOS)
  100.             ismatch = match(++string, ++pattern);
  101.         break;
  102.     case EOS:
  103.         if (*string == EOS)
  104.             ismatch = TRUE;
  105.         break;
  106.     case LEFT_BRACKET:
  107.         if (*string != EOS)
  108.             ismatch = do_list(string, pattern);
  109.         break;
  110.     case BACK_SLASH:
  111.         pattern++;
  112.     default:
  113.         if (*string == ((lcflag && isupper(*pattern)) ? tolower(*pattern) : *pattern)) {
  114.             string++;
  115.             pattern++;
  116.             ismatch = match(string, pattern);
  117.         } else
  118.             ismatch = FALSE;
  119.         break;
  120.     }
  121.     return (ismatch);
  122. }
  123.  
  124.  
  125.  
  126.  
  127.  
  128. /************************/
  129. /*  Function do_list()  */
  130. /************************/
  131.  
  132. static BOOLEAN do_list(string, pattern)
  133. register char *string;
  134. char *pattern;
  135. {
  136.     register BOOLEAN ismatch;
  137.     register BOOLEAN if_found;
  138.     register BOOLEAN if_not_found;
  139.     auto char lower;
  140.     auto char upper;
  141.  
  142.     pattern++;
  143.     if (*pattern == '!') {
  144.         if_found = FALSE;
  145.         if_not_found = TRUE;
  146.         pattern++;
  147.     } else {
  148.         if_found = TRUE;
  149.         if_not_found = FALSE;
  150.     }
  151.     ismatch = if_not_found;
  152.     while (*pattern != ']' && *pattern != EOS) {
  153.         list_parse(&pattern, &lower, &upper);
  154.         if (*string >= lower && *string <= upper) {
  155.             ismatch = if_found;
  156.             while (*pattern != ']' && *pattern != EOS)
  157.                 pattern++;
  158.         }
  159.     }
  160.  
  161.     if (*pattern++ != ']') {
  162.         printf("Character class error\n");
  163.         exit(1);
  164.     } else if (ismatch)
  165.         ismatch = match(++string, pattern);
  166.  
  167.     return (ismatch);
  168. }
  169.  
  170.  
  171.  
  172.  
  173.  
  174. /***************************/
  175. /*  Function list_parse()  */
  176. /***************************/
  177.  
  178. static void list_parse(patp, lowp, highp)
  179. char **patp;
  180. char *lowp;
  181. char *highp;
  182. {
  183.     *lowp = nextch(patp);
  184.     if (**patp == '-') {
  185.         (*patp)++;
  186.         *highp = nextch(patp);
  187.     } else
  188.         *highp = *lowp;
  189. }
  190.  
  191.  
  192.  
  193.  
  194.  
  195. /***********************/
  196. /*  Function nextch()  */
  197. /***********************/
  198.  
  199. static char nextch(patp)
  200. char **patp;
  201. {
  202.     register char ch;
  203.     register char chsum;
  204.     register INT count;
  205.  
  206.     ch = *(*patp)++;
  207.     if (ch == '\\') {
  208.         ch = *(*patp)++;
  209.         if (IS_OCTAL(ch)) {
  210.             chsum = 0;
  211.             for (count = 0; count < 3 && IS_OCTAL(ch); count++) {
  212.                 chsum *= 8;
  213.                 chsum += ch - '0';
  214.                 ch = *(*patp)++;
  215.             }
  216.             (*patp)--;
  217.             ch = chsum;
  218.         }
  219.     }
  220.     return (ch);
  221. }
  222.